<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:color="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>登录注册页面</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.7/css/layui.css">
    <script src="https://cdn.staticfile.org/layui/2.5.7/layui.js"></script>
    <script type="text/javascript" src="/js/jquery-1.8.3.js"></script>
</head>

<body>
<div class="login-container">
    <form class="layui-form" action="/user/login" method="post">	
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" required lay-verify="required" lay-reqtext="请输入用户名"
                       placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" lay-reqtext="请输入密码"
                       placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="login" type="submit">登录</button>
                <!--                lay-submit lay-filter="login"-->
                <button id="register" type="button" class="layui-btn">注册</button>
            </div>
        </div>
    </form>
</div>
<div>
    <form style="display: none; margin-top: 40px; text-align: center;" class="layui-form" action="" id="registerPage">

        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" required lay-verify="required" lay-reqtext="请输入用户名"
                       placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" lay-reqtext="请输入密码"
                       placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 2px;">
                <button class="layui-btn" lay-submit lay-filter="registerData">注册</button>
            </div>
        </div>
    </form>
</div>

<style>
    .login-container {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
    }
</style>
<script th:inline="javascript" type="text/javascript" rel="script">
    layui.use('form', function () {
        let form = layui.form;
        let layer = layui.layer;
        // let message = [[${message}]];
        // layer.msg(JSON.parse('{message}'), {
        //     icon: 2,
        //     time: 3000
        // });

        form.on('submit(registerData)', function (data) {
            let username = data.field.username;
            let password = data.field.password;
            $.post('user/register', {"username": username, "password": password}, (result) => {
                if (result.data == 3) {
                    layer.msg(result.message, {
                        icon: 2,
                        time: 3000
                    });
                } else if (result.data == 1) {
                    layer.msg(result.message, {
                        icon: 1,
                        time: 3000
                    });
                } else {
                    layer.msg(result.message, {
                        icon: 2,
                        time: 3000
                    });
                }
            });
            return false;
        });

        let registerPage;
        $("#register").click(() => {
            $("#registerPage")[0].reset();
            registerPage = layer.open({
                type: 1,
                title: '注册页面',
                content: $("#registerPage"),
                area: ['400px', '300px'],
                shadeClose: false,
                end: function () {
                    $("#registerPage").hide();
                }
            });
        });
    });
</script>
</body>

</html>